using System.Collections.Generic;
using UnityEngine;

public class InGameTutorialObject : EventCommander
{
	public InGametutorialStepSettings[] values = new InGametutorialStepSettings[0];

	public GameObject prefabForEnable;

	public Vector3 prefabLocalPosition;

	public bool MovingParentToTutObject;

	public List<GameObject> enableOnStart = new List<GameObject>();

	public List<GameObject> onObject = new List<GameObject>();

	private bool prefabIsInstantinate;

	public override void Init()
	{
		base.Init();
		base.enabled = false;
		InitEvents(new string[2] { "Used", "ShowThis" });
	}

	public virtual void Use(bool needActive = true)
	{
		for (int i = 0; i < values.Length; i++)
		{
			if (!values[i].started)
			{
				continue;
			}
			values[i].used = true;
			if (needActive)
			{
				enableOnStart.ForEach(delegate(GameObject x)
				{
					x.SetActive(true);
				});
			}
			RiseEvent(this, "Used", values[i].value);
			onObject.ForEach(delegate(GameObject x)
			{
				if (x != null)
				{
					x.SetActive(true);
				}
			});
		}
	}

	public virtual bool OnStepStart(string name)
	{
		for (int i = 0; i < values.Length; i++)
		{
			if (!(values[i].value == name))
			{
				continue;
			}
			values[i].started = true;
			if (values[i].enableOnThisStep)
			{
				if (values[i].unlockThis)
				{
					InGameTutorial.UnblockUIElement(base.gameObject, true);
					base.gameObject.SetActive(true);
				}
				if (prefabForEnable != null && !prefabIsInstantinate)
				{
					enableOnStart.Add(Object.Instantiate(prefabForEnable));
					enableOnStart[enableOnStart.Count - 1].transform.parent = base.transform;
					enableOnStart[enableOnStart.Count - 1].transform.localScale = Vector3.one;
					enableOnStart[enableOnStart.Count - 1].GetComponentInChildren<UIWidget>().depth = 70;
					enableOnStart[enableOnStart.Count - 1].transform.localPosition = prefabLocalPosition;
					prefabIsInstantinate = true;
				}
				if (enableOnStart != null)
				{
					for (int j = 0; j < enableOnStart.Count; j++)
					{
						if (enableOnStart[j] != null)
						{
							enableOnStart[j].SetActive(true);
						}
					}
				}
			}
			if (MovingParentToTutObject)
			{
				for (int k = 0; k < enableOnStart.Count; k++)
				{
					InGameTutorial.UnblockUIElement(enableOnStart[k], true, false);
				}
			}
			return true;
		}
		return false;
	}

	public virtual void OnStepPause(bool val)
	{
		for (int i = 0; i < values.Length; i++)
		{
			if (values[i].started && values[i].enableOnThisStep)
			{
				for (int j = 0; j < enableOnStart.Count; j++)
				{
				}
			}
		}
	}

	public virtual void OnEnd()
	{
		for (int i = 0; i < values.Length; i++)
		{
			if (!values[i].started)
			{
				continue;
			}
			values[i].started = false;
			if (values[i].enableOnThisStep)
			{
				for (int j = 0; j < enableOnStart.Count; j++)
				{
					if (prefabForEnable != null)
					{
						Object.Destroy(enableOnStart[j]);
						enableOnStart.RemoveAt(j);
						prefabIsInstantinate = false;
						j--;
					}
					else if (enableOnStart[j] != null)
					{
						enableOnStart[j].SetActive(false);
					}
				}
				if (values[i].unlockThis)
				{
					RiseEvent(this, "ShowThis", false);
				}
				continue;
			}
			for (int k = 0; k < enableOnStart.Count; k++)
			{
				if (enableOnStart[k] != null)
				{
					enableOnStart[k].SetActive(false);
				}
			}
		}
	}

	private void OnDestroy()
	{
		RemoveAllEventsListeners();
	}
}
